package com.pic.mgt.haishin;

import java.io.File;
import java.io.IOException;

import com.pic.mgt.haishin.executer.AsgmtFileProcess;
import com.pic.mgt.haishin.unit.AppointedAsgmtOkHaishinMessageUnit;
import com.pic.mgt.haishin.unit.AppointedTargetOkHaishinMessageUnit;
import com.pic.mgt.haishin.unit.GeneralTargetOkHaishinMessageUnit;
import com.pic.mgt.monitor.MessageUnitFactory;
import com.pic.mgt.monitor.RedundantConfirmationFileException;
import com.pic.mgt.monitor.UnitConverter;
import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.unit.Message;
import com.pic.mgt.unit.Unit;

public final class HaishinMessageUnitFactory extends MessageUnitFactory {
	private UnitConverter appointedTargetHaishinMessageUnitConverter;
	private UnitConverter appointedAsgmtHaishinMessageUnitConverter;
	private UnitConverter generalHaishinMessageUnitConverter;
	
	@Override
	protected Unit createIGXTypeUnit(File confirmationFile,
									 String messageTypeString,
									 String businessTypeString)
								     throws IOException, WrongUnitFormatException,
								     RedundantConfirmationFileException {
		
		// 先取得空的訊息物件
		Unit messageUnit = getEmptyMessage(messageTypeString, businessTypeString);
		
		if (null != messageUnit) {
			if (AppointedAsgmtOkHaishinMessageUnit.class.isInstance(messageUnit)) {
				// 指示配信交易
				// 交易的是指示配信的指示檔案(位於Haishin Outbo中各端末資料匣底下的)
				
				messageUnit = 
					this.appointedAsgmtHaishinMessageUnitConverter.
									parseToUnit(confirmationFile, messageUnit);
			}
			else if(AppointedTargetOkHaishinMessageUnit.class.isInstance(messageUnit)) {
				// 指示配信交易
				// 交易的是指示配信解出來的檔案(位於Haishin Outbox Public資料匣底下的)
				
				messageUnit = 
					this.appointedTargetHaishinMessageUnitConverter.
									parseToUnit(confirmationFile, messageUnit);
			}
			else if(GeneralTargetOkHaishinMessageUnit.class.isInstance(messageUnit)) {
				// 一般配信交易
				
				messageUnit = 
					this.generalHaishinMessageUnitConverter.
								parseToUnit(confirmationFile, messageUnit);
			}
		}
		else {
			// 找不到對應的訊息類別
			throw new WrongUnitFormatException(
					"Can't find match message type '"
							+ messageTypeString
							+ "' to create message unit for confirmation file '"
							+ confirmationFile.getName() + "'.");
		}
		
		return messageUnit;
	}
	
	private Message getEmptyMessage(String messageTypeString,
									String messgaeIGXTypeString) {
		Message message = null;
		
		if (messageTypeString.endsWith(super.TRANSOK_MESSAGE)) {
			// 如果是屬於 Trans Ok 的 Message
			
			if (messgaeIGXTypeString.equals(HaishinBusinessUnitFactory.GENERAL_HAISHIN)) {
				// 一般配信交易
				// 交易的是 target file 解出來的檔案
				message = new GeneralTargetOkHaishinMessageUnit();
			} 
			else if (messgaeIGXTypeString.equals(HaishinBusinessUnitFactory.APPINTED_HAISHIN)) {
				// 指示配信交易
				// 交易的是指示配信解出來的檔案(位於Haishin Outbox Public資料匣底下的)
				
				message = new AppointedTargetOkHaishinMessageUnit();
			}
			else if (messgaeIGXTypeString.equals(AsgmtFileProcess.typeCharForAsgmtFilename)) {
				// 指示配信交易
				// 交易的是指示配信的指示檔案(位於Haishin Outbo中各端末資料匣底下的)
				
				message = new AppointedAsgmtOkHaishinMessageUnit();
			}
		}
		else {
			/* 如果是屬於 其它的  Message Type (ex: Trans NG ...)
			 * 或表示找不到對應的型態。
			 */
		}
		
		return message;
	}
	
	// --- getter and setter ---

	public void setAppointedTargetHaishinMessageUnitConverter(
			UnitConverter appointedTargetHaishinMessageUnitConverter) {
		this.appointedTargetHaishinMessageUnitConverter = appointedTargetHaishinMessageUnitConverter;
	}

	public void setAppointedAsgmtHaishinMessageUnitConverter(
			UnitConverter appointedAsgmtHaishinMessageUnitConverter) {
		this.appointedAsgmtHaishinMessageUnitConverter = appointedAsgmtHaishinMessageUnitConverter;
	}

	public void setGeneralHaishinMessageUnitConverter(
			UnitConverter generalHaishinMessageUnitConverter) {
		this.generalHaishinMessageUnitConverter = generalHaishinMessageUnitConverter;
	}

}
